package com.occludens.admin.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Load;
import com.googlecode.objectify.annotation.Subclass;

@Subclass(index=true)
@Cache
public class Form extends AdminObjectUI {
	@Load private List<Ref<Field>> fields = new ArrayList<Ref<Field>>();
	
	protected Form() {} // Mandatory for Objectify
	
	public Form(String name) {
		super(name);
	}
	
	public List<Field> getFields() {
		List<Field> returnList = new ArrayList<Field>();
		Iterator<Ref<Field>> fieldsItr = this.fields.iterator();
		while (fieldsItr.hasNext()) {
			returnList.add(fieldsItr.next().get());
		}//End of while fieldsItr
		return returnList;
	}

	public void setFields(List<Field> fields) {
		Iterator<Field> fieldsItr = fields.iterator();
		while (fieldsItr.hasNext()) {
			Ref<Field> fieldRef = Ref.create(fieldsItr.next());
			if (!this.fields.contains(fieldRef)) {
				this.fields.add(fieldRef);
			}
		}//End of while channelsItr
	}

	public void addFields(Field... fields) {
		for (int i=0; i<fields.length; i++) {
			Ref<Field> fieldRef = Ref.create(fields[i]);
			if (!this.fields.contains(fieldRef)) {
				this.fields.add(fieldRef);
			}
		}
	}

	public void removeFields(Field... fields) {
		for (int i=0; i<fields.length; i++) {
			Ref<Field> fieldRef = Ref.create(fields[i]);
			if (this.fields.contains(fieldRef)) {
				this.fields.remove(fieldRef);
			}
		}
	}
}
